import numpy as np
import pandas as pd
from calculate import extract_3x3_matrix, process_radar_data, align_radar_data, extract_to_ndarray,interpolate_layer,calculate_relative_shifts, create_dataset,load_and_process_data

def exam_data(root_path, filename_ZH, filename_CC):
    file_path_ZH = root_path + filename_ZH
    file_path_CC = root_path + filename_CC

    # 加载数据
    data_ZH = pd.read_csv(file_path_ZH)
    data_CC = pd.read_csv(file_path_CC)

    # 利用 cc 数据质控
    filtered_zh = data_ZH.where(data_CC > 0.8, -32768)

    # 分成按层排列的数据
    # layers, layer_1 = process_radar_data(filtered_zh, 1)

    # 归整成三维数组并且计算中间层的线性插值
    angle_ranges = [
        (12000.0, 19000.0),  # 第一段
        (22500.0, 23500.0),  # 第二段
        (25500.0, 26500.0)  # 第三段
    ]

    aligned_data = align_radar_data(filtered_zh, angle_ranges)
    selected_layers = [0, 1, 2, 3, 4]
    # selected_layers = [4, 5, 6, 7, 8]
    selected_range_idx = 0  # 选择第 2 个方位范围（索引从 0 开始）

    array = extract_to_ndarray(aligned_data, selected_layers, selected_range_idx)
    array = array[:, :, 1:]
    # array = np.round(interpolate_layer(array, 2, 3, 2, 1.45, 1.29, 1.79))
    array = np.round(interpolate_layer(array, 3, 4, 3, 4.3, 4.0, 4.5))

    return array

if __name__ == "__main__":
    root_path = 'E:/福建数据/spar/04/test1/'
    filename_ZH = 'Z_RADR_I_Z9600_20240430024720_O_DOR_PAR-SD_CAP_025.bin.gz_ZH.csv'
    filename_CC = 'Z_RADR_I_Z9600_20240430024720_O_DOR_PAR-SD_CAP_025.bin.gz_CC.csv'
    # arr1 = exam_data(root_path, filename_ZH, filename_CC)

    # load_and_process_data(root_path, filename_ZH, filename_CC)
    # print(arr1.shape)
    # arr1_subset = arr1[:, :, 80:1600]

    # for i in range( arr1_subset.shape[0]):
    #     layer = arr1_subset[i]
    #     layer_total = layer.size
    #     layer_invalid = np.sum(layer == -32768)
    #     layer_valid = layer_total - layer_invalid
    #     print(f"\n层 {i}:")
    #     print(f"总点数: {layer_total}")
    #     print(f"无效点数: {layer_invalid}")
    #     print(f"有效点数: {layer_valid}")
    #     print(f"有效点比例: {(layer_valid / layer_total) * 100:.2f}%")
    #
    #     # 打印该层的一些样本值
    #     print(f"数据样本(第一行前5个值):")
    #     print(layer[0, :5])
    # count1 = 0
    # for i in range(arr1_subset.shape[0]):
    #     for j in range(arr1_subset.shape[1]):
    #         for k in range(arr1_subset.shape[2]):
    #             if (arr1_subset[i,j,k] == -32768):
    #                 count1 = count1 + 1
    # print(count1)
    data_ZH = pd.read_csv(root_path+filename_ZH, header=None)
    data_CC = pd.read_csv(root_path+filename_CC, header=None)
    data1 = data_ZH.where(data_CC > 0.8, -32768)
    angle_ranges = [
        (12000.0, 19000.0),  # 第一段
        (22500.0, 23500.0),  # 第二段
        (25500.0, 26500.0)  # 第三段
    ]
    aligned_data = align_radar_data(data1, angle_ranges)
    selected_layers = [0, 1, 2, 3, 4]
    # selected_layers = [4, 5, 6, 7, 8]
    selected_range_idx = 0  # 选择第 2 个方位范围（索引从 0 开始）

    array = extract_to_ndarray(aligned_data, selected_layers, selected_range_idx)
    array = array[:, :, 1:]
    array = np.round(interpolate_layer(array, 2, 3, 2, 1.45, 1.29, 1.79))
    all_distances = np.linspace(0, 200, 3200)  # 全部距离范围
    selected_distances = all_distances[80:1600]  # 选择第 80 到第 1600 个库
    elvs = [0.5, 0.89, 1.45, 2.4]
    # elvs = [2.4, 3.0, 3.5, 4.3]
    distance_shifts = calculate_relative_shifts(selected_distances, elvs)

    # 使用 create_dataset 提取特征和标签
    feature_data, label_data = create_dataset(array, distance_shifts, max_bins=1520)
    # data2 = data1.iloc[0:78,80:1600]
    # data2 = np.array(data2)
    array = array[:,:,80:1600]
    print(f"\n提取后数组形状: {array.shape}")
    print(f"无效值数量: {(array[:, :, 80:1600] == -32768).sum()}")
    count = 0
    # for i in range(array.shape[1]):
    #     for j in range(array.shape[2]):
    #         if (array[4,i,j] == -32768):
    #             count = count + 1
    # # #
    print(array.shape)
    print(count)
